package be.vdabantwerpen.gow.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import be.vdabantwerpen.gow.entities.GOWUser;
import be.vdabantwerpen.gow.services.PostService;
import be.vdabantwerpen.gow.services.PostServiceImpl;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import java.util.Collections;

public class PostStreamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String VIEW = "/WEB-INF/JSP/posts/poststream.jsp";
	private final PostService postService = new PostServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GOWUser gowUser = null;
		HttpSession session = request.getSession();
		
		if (session != null) {
			gowUser = (GOWUser) session.getAttribute("user");
			if (gowUser != null) {
				request.setAttribute("user", gowUser);
			}
		}
		
		request.setAttribute("posts", postService.findMostRecent());
        request.getRequestDispatcher(VIEW).forward(request, response);
	}
	
}
